<?php
namespace app\index\controller;

use think\Request;
use think\Controller;
use think\Session;
use think\Cookie;

//小店、代理端
class Agentshop extends Common{
    public function index(){
		$response = \ServerHelper::post('agentshop_index',[
			'agentid' => AGENT_ID
		]);
		$data = $this->J($response);
		return view('',$data['data']);
    }
	//店铺推广连接
	public function links(){
		$request = Request::instance();
		$link = $request->domain() . '/shop/?shop_id=' . AGENT_ID;
		return view('',[
			'link' => $link
		]);
	}
	//店铺推广码
	public function code(){
		if(Cookie::has('agentshopcode')){
			$path = Cookie::get('agentshopcode');
			if(file_exists($path)){
				/*
				header('Content-type: image/png');
				$img = imagecreatefrompng($path);
				imagepng($img);
				imagedestroy($img);
				exit();
				*/
				
				$image_info = getimagesize($path);
$base64_image_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($path)));
				
				return view('',['data'=>$base64_image_content]);
				
			}
		}
		$response = \ServerHelper::post('agentshop_index',[
			'agentid' => AGENT_ID
		]);
		$data = $this->J($response);
		if(empty($data['flag'])){
			__log(AGENT_ID . json_encode($data,JSON_UNESCAPED_UNICODE),'agentshop_code');
			exit();
		}
		$info = $data['data'];
		$shopname = $info['agentinfo']['shopname'];
		$headimgurl = $info['agentinfo']['headimage'];
		
		$request = Request::instance();
		$link = $request->domain() . '/shop/?shop_id=' . AGENT_ID; //店铺推广链接
		
		Vendor('CreateAgentShopCode.CreateAgentShopCode');
		$hp = new \CreateAgentShopCode();
		try{
			$res = $hp->create([
				'shopname' => $shopname,
				'headimg' => $headimgurl,
				'link' => $link,
			]);
			if(is_array($res) && count($res) == 3 && $res[0] == true){
				Cookie::set('agentshopcode',$res[2],180);
				/*
				header('Content-type: image/png');
				$img = imagecreatefrompng($res[2]);
				imagepng($img);
				imagedestroy($img);
				exit();
				*/
				$path = $res[2];
				$image_info = getimagesize($path);
$base64_image_content = "data:{$image_info['mime']};base64," . chunk_split(base64_encode(file_get_contents($path)));
				
				return view('',['data'=>$base64_image_content]);
				
			}
		}catch(\Exception $e){
			__log($e->getMessage(),'agentshop_code');
		}
	}
	
	#region 店铺信息
	
	//代理信息
	public function agentinfo(){
		$request = Request::instance();
		if($request->isPost()){
			$act = $request->param('act');
			$action = '';
			switch($act){
				case 'shop_name' : 
					$action = 'shopname';
					break;
				//case 'business_license' : 
				//	$action = 'buslicense';
				//	break;
				case 'mobile' : 
					$action = 'PhoneNum';
					break;
				case 'wechat' : 
					$action = 'wechatid';
					break;
			}
			if($action == ''){
				ajax('未知操作');
			}
			$value = $request->param('value');
			if($value == ''){
				ajax('设置不能为空');
			}
			$response = \ServerHelper::post('agentshop_agentinfo_save',[
				'agentid' => AGENT_ID,
				'action' => $action,
				'value' => $value,
			]);
			$data = $this->J($response);
			if(!empty($data['flag'])){
				ajax('保存成功',true);
			}
			ajax('保存失败');
			
		}else{
			$response = \ServerHelper::post('agentshop_index',[
				'agentid' => AGENT_ID
			]);
			$data = $this->J($response);
			return view('',$data['data']);
		}
	}
	
	//点击转发
	public function share(){
		$response = \ServerHelper::post('GetWeChatConfig');
		$data = json_decode($response,true);
		if(empty($data['appid'])){
			$this->error('数据错误');
		}
		//微信配置
		$hp = new \WechatBase($data['appid'],$data['appsecret']);
		$param = $hp->getSignPackage();
		$data = [
			'data' => $param
		];
		//代理信息
		$response = \ServerHelper::post('agenthome',['agentid'=>AGENT_ID]);
		$agent = $this->J($response);
		$data = [
			'wx' => $param,
			'agent' => $agent['data']['agent'],
			'shopurl' => Request::instance()->domain() . '/shop/?shop_id=' . AGENT_ID
		];
		return view('',$data);
	}
	
	//营业执照
	public function business_license(){
		$request = Request::instance();
		if($request->isPost()){
			if(!isset($_FILES["image"]["error"])){
				ajax('请选择图片');
			}
			if($_FILES["image"]["error"] == 1){
				ajax('图片尺寸过大');
			}else if($_FILES["image"]["error"] != 0){
				ajax('图片上传失败');
			}
			$extention = explode('.',$_FILES['image']['name']);
			$extention = end($extention);
			$allow = ['jpg','jpeg','pjpeg','gif','png'];
			if(!in_array(strtolower($extention),$allow)){
				ajax('文件格式不正确');
			}
			#endregion
			
			$ali = controller('Aliyun');
			$data = $ali->frontupload();

			if(!empty($data['flag'])){
				$response = \ServerHelper::post('agentshop_agentinfo_save',[
					'agentid' => AGENT_ID,
					'action' => 'buslicense',
					'value' => $data['data'],
				]);
				$data = $this->J($response);
				if(!empty($data['flag'])){
					ajax('保存成功',true);
				}
			}
			ajax('保存失败');
			
		}else{
			$response = \ServerHelper::post('agentshop_index',[
				'agentid' => AGENT_ID
			]);
			$data = $this->J($response);
			return view('',$data['data']);
		}
	}
	
	#endregion

	//代理查看下级订单列表
	public function orders(){
		$request = Request::instance();
		$param = [
			'agentid' => AGENT_ID,
			'status' => $request->param('status',-1,'intval'),
			'substatus' => $request->param('substatus',-1,'intval'),
			'key' => $request->param('key'),
			'page' => $request->param('page',1,'intval'),
		];
		$response = \ServerHelper::post('agentshop_orderlist',$param);
		$data = $this->J($response);
		//var_dump($data['data']);
		return view('',$data['data']);
	}
	//加载订单页
	public function order_loadpage(){
		$request = Request::instance();
		$param = [
			'agentid' => AGENT_ID,
			'status' => $request->param('status',-1,'intval'),
			'substatus' => $request->param('substatus',-1,'intval'),
			'key' => $request->param('key'),
			'page' => $request->param('page',1,'intval'),
		];
		$response = \ServerHelper::post('agentshop_orderlist',$param);
		$data = $this->J($response);
		$info = $data['data'];
		$list = [];
		foreach($info['orderinfo'] as $k=>$v){
			$one = $v;
			$one['Status'] = __order_status3($v['Status'],$v['OrderType']);
			$one['buttons'] = __order_status_btns_3($v['Status'],$v['OrderType']);
			$list[] = $one;
		}
		$data = [
			'list' => $list,
			'status' => $info['search']['status'],
			'substatus' => $info['search']['substatus'],
			'key' => $info['search']['key'],
			'page' => $info['search']['page'],
		];
		ajax('success',true,$data);
	}
	
	//订单详情
	public function orderdetail(){
		$request = Request::instance();
		$param = [
			'agentid' => AGENT_ID,
			'orderid' => $request->param('id'),
		];
		$response = \ServerHelper::post('agentshop_orderdetail',$param);
		$data = $this->J($response);
		return view('',$data['data']);
	}
	//订单打款凭证
	public function order_checkrep(){
		$request = Request::instance();
		$param = [
			'agentid' => AGENT_ID,
			'orderid' => $request->param('orderid'),
		];
		$response = \ServerHelper::post('agentshop_ordercheckrep',$param);
		$data = $this->J($response);
		return view('',$data['data']);
	}
	
	//打款审核(同意、驳回)
	public function order_check(){
		/*
		agentid
		customerid
		orderid
		res  (1：同意；2：驳回)
		pwd
		remark
		*/
		$request = Request::instance();
		$param = [
			'agentid' => AGENT_ID,
			'orderid' => $request->param('orderid'),
			'customerid' => $request->param('customerid'),
		];
		$act = $request->param('act');
		if($act == 'confirm'){
			$param['res'] = 1;
			$param['pwd'] = $request->param('info');
		}else{
			$param['res'] = 2;
			$param['remark'] = $request->param('info');
		}
		$response = \ServerHelper::post('agentshop_orderpaycheck',$param);
		$data = json_decode($response,true);
		if(!isset($data['flag'])){
			ajax('网络错误');
		}
		if(empty($data['flag'])) ajax($data['msg']);
		ajax($data['msg'],$data['flag'] == 1);
	}
	
	//零售订单物流信息
	public function order_logistics(){
		$request = Request::instance();
		$param = [
			'orderid' => $request->param('orderid'),
			'agentid' => AGENT_ID,
		];
		$response = \ServerHelper::post('agentshop_logistics',$param);
		$data = $this->J($response);
		if(!isset($data['msg'])){
			$this->error('数据错误');
		}else if($data['flag'] == 0){
			$this->success($data['msg']);
		}else{
			return view('',$data);
		}
	}
	
	//代理自己发货的时候 先校验库存
	public function order_checkstock(){
		$request = Request::instance();
		$param = [
			'orderid' => $request->param('orderid'),
			'customerid' => $request->param('customerid'),
			'agentid' => AGENT_ID,
		];
		$response = \ServerHelper::post('agentshop_ordercheckstock',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			ajax('数据错误');
		}
		ajax($data['msg'],$data['flag'] == 1);
	}
	
	//代理自己给下级发货
	public function order_deliver(){
		$request = Request::instance();
		if($request->isPost()){
			$param = [
				'orderid' => $request->param('orderid'),
				'customerid' => $request->param('customerid'),
				'agentid' => AGENT_ID,
				'expresscomid' => $request->param('expresscomid'),
				'expressid' => $request->param('expressid'),
			];
			$response = \ServerHelper::post('agentshop_deliverdo',$param);
			$data = $this->J($response);
			ajax($data['msg'],$data['flag'] == 1);
			
		}else{
			$param = [
				'orderid' => $request->param('orderid'),
				'customerid' => $request->param('customerid'),
				'agentid' => AGENT_ID,
			];
			$response = \ServerHelper::post('agentshop_delivershow',$param);
			$data = $this->J($response);
			
			$express_list = [];
			foreach($data['data']['expressList'] as $k=>$v){
				$express_list[] = [
					'id' => $v['expresscomid'],
					'value' => $v['expresscomname'],
				];
			}
			$data['data']['kuaidi'] = $express_list;
			return view('',$data['data']);
		}
	}
	
	//确认下级订单收货
	public function order_receipt(){
		$request = Request::instance();
		$param = [
			'orderid' => $request->param('orderid'),
			'customerid' => $request->param('customerid'),
			'agentid' => AGENT_ID,
		];
		$response = \ServerHelper::post('agentshop_receipt',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			ajax('数据错误');
		}
		ajax($data['msg'],$data['flag'] == 1);
	}
	
	//一键代发提示
	public function yjdf_tip(){
		$request = Request::instance();
		$param = [
			'orderid' => $request->param('orderid'),
			'agentid' => AGENT_ID,
			'customerid' => $request->param('customerid'),
		];
		$response = \ServerHelper::post('agentshop_yjdf_tip',$param);
		$data = $this->J($response);
		if($data['flag']){
			$data['data']['pay'] = number_format($data['data']['pay'],2);
			$data['data']['in'] = number_format($data['data']['in'],2);
		}
		ajax($data['msg'],$data['flag'] == 1,$data['data']);
	}
	
	//一键代发Confirm
	public function yjdf_confirm(){
		$request = Request::instance();
		$param = [
			'orderid' => $request->param('orderid'),
			'customerid' => $request->param('customerid'),
		];
		$response = \ServerHelper::post('agentshop_yjdf_confirm',$param);
		
		$data = $this->J($response);
				
		//转中文键
		$product = [];
		$orderamount = 0;//商品总价
		foreach($data['data']['订单详情'] as $v){
			$product[] = [
				'productmainimg' => $v['产品主图'],
				'productname' => $v['产品名称'],
				'productprice' => $v['产品价格'],
				'buynum' => $v['产品数量'],
			];
			$orderamount+=$v['产品价格'] * $v['产品数量'];
		}
		$data['data']['product'] = $product;
		$data['data']['orderamount'] = $orderamount;
		//快递公司
		$express_list = [];
		$express_default = [];
		$express_listfull = [];
		foreach((array)$data['data']['快递信息'] as $k=>$v){
			$express_list[] = [
				'id' => $v['expressid'],
				'value' => $v['expresscompany'],
			];
			$express_listfull[$v['expressid']] = [
				'id' => $v['expressid'],
				'value' => $v['expresscompany'],
				'expressprice' => $v['expressprice'],
			];
			if($k== 0){
				$express_default = [
					'id' => $v['expressid'],
					'value' => $v['expresscompany'],
					'expressprice' => $v['expressprice'],
					'index' => 0
				];
			}
		}

		$data['express'] = [
			'list' => json_encode($express_list,JSON_UNESCAPED_UNICODE),
			'default' => $express_default,
			'listfull' => json_encode($express_listfull,JSON_UNESCAPED_UNICODE),
		];
		
		$data['data']['address'] = [
			'consignee' => $data['data']['地址详情']['收货人姓名'],
			'phonenum' => $data['data']['地址详情']['手机号码'],
			'province' => $data['data']['地址详情']['省'],
			'city' => $data['data']['地址详情']['市'],
			'region' => $data['data']['地址详情']['区'],
			'address' => $data['data']['地址详情']['详细地址'],
		];
		$data['data']['orderid'] = $data['data']['订单号'];
		return view('',$data);
	}
	//转代理订单
	public function yjdf_inorder(){
		$request = Request::instance();
		$param = [
			'agentid' => AGENT_ID,
			'orderid' => $request->param('orderid'),
			'expresscomid' => $request->param('expressid'),
			'remark' => $request->param('remark'),
		];
		$response = \ServerHelper::post('agentshop_yjdf_inorder',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			ajax(isset($data['msg']) ? $data['msg'] : '订单提交失败');
		}
		
		ajax($data['msg'],true,$data['data']['orderid']);
	}
	
}
